package com.kcl.dfss;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.kcl.dfss.http.ServerConnection;
import com.kcl.dfss.utils.AppUtils;
import com.kcl.dfss.utils.ApplicationData;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.Preference;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AboutAppActivity extends Activity {

	private LinearLayout about_back;
	private TextView text_version;
	private ListView list_about;
	
	private String[] title = {"服务热线","官方网站","检测最新版本","使用条款和隐私策略"};
	private String[] content = {"400-163-0002","www.roadefend.com","",""};
	private int image = R.drawable.arrow_right;
	private String app_version = null;
	private String userid = null;
	private String server_version,appcontent,appurl;
	private Handler handler = null;
	private boolean isenable = false;
	private boolean networkConnect = false;
	private List<Map<String, Object>> listItems = null;
	SharedPreferences preference = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_about_app);
		
		about_back = (LinearLayout) findViewById(R.id.about_back);
		text_version = (TextView) findViewById(R.id.text_version);
		list_about = (ListView) findViewById(R.id.list_about);
		
		app_version = ApplicationData.app_version;
		text_version.setText("版本号"+app_version);
		
		preference = getSharedPreferences("shared_login", 0);
		userid = preference.getString("userid", "");
		
		about_back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				if(msg.what == 0){
					if(app_version.equals(server_version)){
						content[2] = "当前已是最新版本";
						listItems = new ArrayList<Map<String,Object>>();
						for(int i=0;i<title.length;i++){
							Map<String, Object> listItem = new HashMap<String, Object>();
							listItem.put("title", title[i]);
							listItem.put("content", content[i]);
							listItem.put("image", image);
							listItems.add(listItem);
						}
					}else{
						content[2] = "有新版本可以更新";
						listItems = new ArrayList<Map<String,Object>>();
						for(int i=0;i<title.length;i++){
							Map<String, Object> listItem = new HashMap<String, Object>();
							listItem.put("title", title[i]);
							listItem.put("content", content[i]);
							listItem.put("image", image);
							listItems.add(listItem);
						}
						isenable = true;
					}
				}else{
					content[2] = "当前版本"+app_version;
					listItems = new ArrayList<Map<String,Object>>();
					for(int i=0;i<title.length;i++){
						Map<String, Object> listItem = new HashMap<String, Object>();
						listItem.put("title", title[i]);
						listItem.put("content", content[i]);
						listItem.put("image", image);
						listItems.add(listItem);
					}
				}
				
				SimpleAdapter simpleAdapter = new SimpleAdapter(AboutAppActivity.this, listItems, R.layout.device_item1,
						new String[] {"title","content","image"}, new int[] {R.id.text_machine,R.id.text_machinename,R.id.image_machine});
				list_about.setAdapter(simpleAdapter);
				list_about.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						switch (position) {
						case 0:
							Intent intentPhone = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:"+"4001630002"));
							startActivity(intentPhone);
							break;
						case 1:
							Uri uri = Uri.parse("http://www.roadefend.com");
							Intent intentView = new Intent(Intent.ACTION_VIEW,uri);
							//加下面这句话就是启动系统自带的浏览器打开上面的网址，  不加下面一句话，  如果你有多个浏览器，就会弹出让你选择某一浏览器， 然后该浏览器就会打开该网址...............
							//intentView.setClassName("com.android.browser", "com.android.browser.BrowserActivity");
							startActivity(intentView);
							break;
						case 2:
							if(isenable){
								Intent intent = new Intent();
								intent.putExtra("server_version", server_version);
								intent.putExtra("appcontent", appcontent);
								intent.putExtra("appurl", appurl);
								intent.setClass(AboutAppActivity.this, UpdateAppActivity.class);
								startActivity(intent);
							}else{
								
							}
							break;
						case 3:
							startActivity(new Intent(AboutAppActivity.this, AppPrivacyActivity.class));
							break;
						default:   
							break;
						}
					}
				});
			}
			
		};
		networkConnect = AppUtils.isNetworkAvailable(AboutAppActivity.this);
		if(networkConnect){
			getAppVersion();
		}else{
			Message message = new Message();
			message.what = 1;
			handler.sendMessage(message);
		}
	}
	
	public void getAppVersion(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				String result = ServerConnection.GetApp(userid);
				try {
					JSONObject jsonObject = new JSONObject(result);
					String success = jsonObject.getString("success");
					String msg = jsonObject.getString("msg");
					if(success.equals("true")){
						String data = jsonObject.getString("data");
						JSONObject object = new JSONObject(data);
						server_version = object.getString("appversion");
						appcontent = object.getString("appcontent");
						appurl = object.getString("appurl");
						Message message = new Message();
						message.what = 0;
						handler.sendMessage(message);
					}else{
						Message message = new Message();
						message.what = 1;
						handler.sendMessage(message);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}).start();
	}

}
